package priv.pront.code.mati.province.comp;

import java.util.Scanner;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-11-18 14:21
 */
public class Main4 {

    static int a, b, n = 0, len;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        a = scanner.nextInt();
        b = scanner.nextInt();
        len = (a + "").length();
        for (int i = 0; i < len; i++) {
            if (huzhi(a, b) != 1) {
                change();
            }else{
                System.out.println(i);
                return;
            }
        }
        System.out.println(-1);
    }


    private static int huzhi(int a, int b) {
        if(a % b == 0){
            return b;
        }
        return huzhi(b,a % b);
    }

    private static void change(){
        String aa = a + "";
        String bb = b + "";
        StringBuilder sba = new StringBuilder(aa.substring(1, aa.length()));
        StringBuilder sbb = new StringBuilder(bb.substring(1, bb.length()));
        sba.append(aa.charAt(0));
        sbb.append(bb.charAt(0));
        a = Integer.parseInt(sba.toString());
        b = Integer.parseInt(sbb.toString());
    }
}
